import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Arc, ConnectionPatch, Polygon

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimSun']
plt.rcParams['axes.unicode_minus'] = False

# 设置图形
plt.figure(figsize=(10, 8))
ax = plt.subplot(111)

# 绘制单位圆
circle = plt.Circle((0, 0), 1, fill=False, color='black', linewidth=1.5)
ax.add_patch(circle)

# 设置坐标轴
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.xlim(-1.2, 1.5)
plt.ylim(-0.2, 1.5)
plt.gca().set_aspect('equal', adjustable='box')

# 隐藏坐标轴标尺
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)

# 定义角度 x (以弧度计)
x = 0.7

# 计算点坐标
O = (0, 0)  # 圆心
A = (1, 0)  # 圆与x轴交点
D = (np.cos(x), np.sin(x))  # 圆上点，将P改为D
C = (np.cos(x), 0)  # D在x轴上的投影
B = (1, np.tan(x))  # 切线与射线的交点

# 延长OD线到B点
plt.plot([O[0], B[0]], [O[1], B[1]], 'b-', linewidth=1.5)  # OD延长到B

# 绘制切线
plt.plot([0.7, 1.3], [0, 0], 'k-', linewidth=1.5)  # x轴部分
plt.plot([1, 1], [0, 1.3], 'k-', linewidth=1.5)  # 切线

# 绘制弧线
arc = Arc(O, 2, 2, theta1=0, theta2=np.degrees(x), color='purple', linewidth=1.5)
ax.add_patch(arc)

# 标记点
plt.plot(O[0], O[1], 'ko', markersize=5)
plt.plot(A[0], A[1], 'ko', markersize=5)
plt.plot(D[0], D[1], 'ko', markersize=5)
plt.plot(C[0], C[1], 'ko', markersize=5)
plt.plot(B[0], B[1], 'ko', markersize=5)

# 添加文本标签
plt.text(O[0]-0.05, O[1]-0.05, '$O$', fontsize=12, style='italic')
plt.text(A[0]+0.02, A[1]-0.05, '$A$', fontsize=12, style='italic')
# 将P改为D，并调整位置
plt.text(D[0]+0.02, D[1]+0.05, '$D$', fontsize=12, style='italic')
plt.text(B[0]+0.02, B[1]+0.02, '$B$', fontsize=12, style='italic')
# 添加C点标签
plt.text(C[0]+0.02, C[1]-0.05, '$C$', fontsize=12, style='italic')

# 添加线段标签
plt.text((O[0]+A[0])/2, (O[1]+A[1])/2-0.05, '$1$', fontsize=10, color='blue', style='italic')
# 使用绿色虚线突出显示sinx线段
plt.plot([D[0], C[0]], [D[1], C[1]], 'g--', linewidth=2.5, alpha=0.8)
plt.text((D[0]+C[0])/2-0.05, (D[1]+C[1])/2, r'$\sin x$', fontsize=12, color='green', style='italic')
plt.text((A[0]+B[0])/2+0.05, (A[1]+B[1])/2, r'$\tan x$', fontsize=12, color='red', style='italic')

# 添加角度标记（只保留一个x）
angle_arc = Arc(O, 0.3, 0.3, theta1=0, theta2=np.degrees(x), color='black', linewidth=1)
ax.add_patch(angle_arc)
plt.text(0.15, 0.05, r'$x$', fontsize=12, style='italic')

# 添加极限公式（移除左上角标签）
plt.text(-1.1, 1.4, r'$\lim_{x \to 0} \frac{\sin x}{x} = 1$', fontsize=14,
         bbox=dict(facecolor='white', alpha=0.8))

# 添加颜色填充区域
# 三角形DOA - 蓝色
triangle_doa = Polygon([O, D, A], closed=True, color='blue', alpha=0.2)
ax.add_patch(triangle_doa)

# 扇形DOA - 绿色
theta = np.linspace(0, x, 100)
sector_points = np.array([(0,0)] + [(np.cos(t), np.sin(t)) for t in theta] + [(0,0)])
sector = Polygon(sector_points, closed=True, color='green', alpha=0.2)
ax.add_patch(sector)

# 大三角形AOB - 红色
triangle_aob = Polygon([O, A, B], closed=True, color='red', alpha=0.2)
ax.add_patch(triangle_aob)

plt.tight_layout()
plt.savefig('unit_circle_limit.png', dpi=300)
plt.show()